// pages/discover/discover.js
const config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: config.basePath,
    categoryList: [],
    articleLoding: true,
    articleData: [],
    total: 0,
    pageIndex: 1,
    currCategoryId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "发现"
    })
    this.getCategoryList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      categoryList: [],
      articleData: [],
      total: 0,
      articleLoding: true,
      pageIndex: 1
    });
    that.getCategoryList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.total <= that.data.articleData.length) {
      return;
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1
    });
    that.getArticleList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onShareTimeline(res) {
    return {
      title: config.miniName + '-发现',
      path: '/pages/discover/discover'
    }
  },

  getCategoryList: function () {
    let that = this;
    wx.request({
      url: config.basePath + '/mini/category/getAllList',
      method: 'GET',
      success(res) {
        if (res.data.code === 200) {
     
          that.setData({
            categoryList: res.data.data,
            currCategoryId: res.data.data[0].id
          });
          if (res.data.data.length > 0) {
            that.getArticleList();
          }
        } else {
          wx.lin.showMessage({
            type: 'error',
            content: '服务出错'
          })
        }
      },
      fail(err) {
        wx.lin.showMessage({
          type: 'error',
          content: '服务出错'
        })
      }
    });
  },

  tabChange: function (e) {
    let that = this;
    that.setData({
      articleData: [],
      total: 0,
      articleLoding: true,
      pageIndex: 1,
      currCategoryId: e.detail.activeKey
    });
    that.getArticleList();
  },

  getArticleList: function () {
    let that = this;
    that.setData({
      articleLoging: true
    });
    wx.request({
      url: config.basePath + '/mini/article/list',
      method: 'GET',
      data: {
        pageIndex: that.data.pageIndex,
        pageSize: 6,
        categoryId: that.data.currCategoryId
      },
      success(res) {
        if (res.data.code === 200) {
          let articleData=  res.data.data.map(function(item){
            let data=item
             let s= config.basePath+ item.thumbnail
            console.log(s)
            let b=item.thumbnail
            if(!(item.thumbnail.slice(0,4)=='http')){
              s= config.basePath+ item.thumbnail
            }else{
             s=  item.thumbnail
             console.log('s1',item)
            }
            if(s==config.basePath){
              console.log('随机图片')
              s='https://api.vvhan.com/api/bing?rand=sj'
            }
            data.thumbnail=s
            return data
            })
          that.setData({
            articleData: that.data.articleData.concat(articleData),
            total: res.data.total,
            articleLoding: true
          });

          if (that.data.total <= that.data.articleData.length) {
            that.setData({
              articleLoding: false
            });
          }
        } else {
          wx.lin.showMessage({
            type: 'error',
            content: '服务出错'
          })
        }
      },
      fail(err) {
        that.setData({
          articleLoding: false
        });
        wx.lin.showMessage({
          type: 'error',
          content: '服务出错'
        })
      }
    });
  },

  search: function(e) {
    wx.navigateTo({
      url: "/pages/search/search?title=" + e.detail.value
  })
  }
})